<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Index</title>
<script
			  src="https://code.jquery.com/jquery-1.12.4.js"
			  integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
			  crossorigin="anonymous"></script>
</head>
<body>
	
	<div id="message"></div>
	<input type="text" id="data">
	<button onclick="send()">发送</button>
</body>
</html>
<script>
	var webSocket;
	var h=window.location.host;
	if (window.WebSocket) {
		webSocket = new WebSocket("ws://"+h+"/");

		//连通之后的回调事件  
		webSocket.onopen = function() {
			webSocket.send("发送数据");
		};

		//接收后台服务端的消息  
		webSocket.onmessage = function(evt) {
			var received_msg = evt.data;
		$("#message").html("数据已接收:" + received_msg);
		};

		//连接关闭的回调事件  
		webSocket.onclose = function() {
			alert("连接已关闭...");
		};

	}
	function send(){
		var d=$("#data").val();
		if(d){
			webSocket.send(d);
		}
		
	}
</script>






































































